package com.heima.article.service.impl;

import com.heima.api.IArticleClient;
import com.heima.api.admin.IBehaviorClient;
import com.heima.api.admin.IUserClient;
import com.heima.article.service.ApArticleCollectionService;
import com.heima.article.service.ApArticleInfoService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.vos.ArticleInfoVo;
import com.heima.model.behavior.dtos.IsCollectionDto;
import com.heima.model.behavior.dtos.IsFollowDto;
import com.heima.model.behavior.dtos.IsLikeDto;
import com.heima.model.behavior.dtos.IsUnlikeDto;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class ApArticleInfoServiceImpl implements ApArticleInfoService {

    private final ApArticleCollectionService apArticleCollectionService;

    private final IBehaviorClient behaviorClient;
    private final IUserClient userClient;

    @Override
    public ArticleInfoVo loadArticleInfo(ArticleInfoDto articleInfoDto) {
        /* 通过传入的 articleId(文章id) 与 authorId(作者id) 返回数据 */
        Integer userId = WmThreadLocalUtil.getUser().getId();

        /* 返回 是否点赞该文章 */
        // 根据 userId 与 articleId 查询 ap_article_like 表
        IsLikeDto isLikeDto = new IsLikeDto(userId.longValue(), articleInfoDto.getArticleId());
        Boolean islike = behaviorClient.isLike(isLikeDto);


        /* 返回 是否不喜欢该文章 */
        // 根据  userId 与 articleId 查询 ap_article_unlike 表
        IsUnlikeDto isUnlikeDto = new IsUnlikeDto(userId.longValue(), articleInfoDto.getArticleId());
        Boolean isunlike = behaviorClient.isUnlike(isUnlikeDto);

        /* 返回 是否收藏  */
        // 根据 userId 与 articleId 查询 ap_collection 表
        IsCollectionDto isCollectionDto = new IsCollectionDto(userId.longValue(), articleInfoDto.getArticleId());
        Boolean iscollection = apArticleCollectionService.isCollection(isCollectionDto);


        /* 是否关注该文章作者 */
        IsFollowDto isFollowDto = new IsFollowDto(userId.longValue(), articleInfoDto.getAuthorId());
        Boolean isfollow = userClient.isFollow(isFollowDto);

        ArticleInfoVo articleInfoVo = new ArticleInfoVo(islike, isunlike, iscollection, isfollow);
        return articleInfoVo;
    }
}
